package com.jjwxc.jwjskandriod.fragment;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.activity.FindActivity;
import com.jjwxc.jwjskandriod.adapter.SuggestAdapter;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.readActivity.utils.UMConstant;
import com.umeng.analytics.MobclickAgent;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class SuggestFragment extends Fragment {

    private View view;
    /**
     * 菜单标题
     */
    private final String[] TAB_TITLES = new String[]{"推荐", "言情", "纯爱", "衍生"};

    ViewPager viewPager;
    TabLayout tabLayout;
    private LinearLayout ll_bg_vip;
    private ImageView img_find;
    /**
     * 页卡适配器
     */
    private SuggestAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_suggest, container, false);
            viewPager = view.findViewById(R.id.view_suggest_pager);
            tabLayout = view.findViewById(R.id.tab_suggest_layout);
            img_find = view.findViewById(R.id.img_find);
            ll_bg_vip = view.findViewById(R.id.ll_bg_vip);
            setTabs(tabLayout, getLayoutInflater(), TAB_TITLES);
            initPager();
            clickListener();
        }
        return view;
    }


    private void initPager() {
        adapter = new SuggestAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        LinearLayout linearLayout = (LinearLayout) tabLayout.getTabAt(0).getCustomView();
        TextView textView = (TextView) linearLayout.getChildAt(0);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        // 关联切换
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 取消平滑切换
                viewPager.setCurrentItem(tab.getPosition(), false);
                int s = tabLayout.getTabCount();
                for (int i = 0; i < s; i++) {
                    if (tab.getPosition() == i) {
                        LinearLayout linearLayout = (LinearLayout) tabLayout.getTabAt(i).getCustomView();
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        LinearLayout linearLayout = (LinearLayout) tabLayout.getTabAt(i).getCustomView();
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }


            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (PreUtils.getInt("app_bg", 0) == 0) {
            ll_bg_vip.setBackgroundResource(R.mipmap.vip_bg_one);
        } else if (PreUtils.getInt("app_bg", 0) == 2) {
            ll_bg_vip.setBackgroundResource(R.mipmap.vip_bg_two);
        } else if(PreUtils.getInt("app_bg", 0) == 1){
            ll_bg_vip.setBackgroundResource(R.mipmap.vip_bg_three);
        }else {
            ll_bg_vip.setBackgroundResource(R.mipmap.vip_bg_moren);
        }
        MobclickAgent.onEvent(getContext(),UMConstant.SUGGEST_TUIJIAN);
    }

    /**
     * 设置页卡显示效果
     *
     * @param tabLayout
     * @param inflater
     * @param tabTitlees
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater, String[] tabTitlees) {
        for (int i = 0; i < tabTitlees.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.suggest_item_fragment, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            TextView tvTitle = (TextView) view.findViewById(R.id.txt_suggest_tab);
            tvTitle.setText(tabTitlees[i]);
            tabLayout.addTab(tab);
        }
    }

    public void clickListener() {
        img_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), FindActivity.class);
                startActivity(intent);
            }
        });
    }
}
